import React, { createContext, useReducer } from 'react';

const RecStateContext = createContext();

const reducer = (state, action) => {
    switch (action.type) {
        case 'setUrl':
            return {
                ...state,
                url: action.payload
            };
        case 'setMainRECState':
            return {
                ...state,
                isMainRecording: action.payload
            };
        case 'setIsFirstQuestionREC': // 设置是否为首次点击问题录音
            return {
                ...state,
                isFirstQuestionREC: action.payload
            };
        default:
            return state;
    }
};

const initState = {
    url: '', // 面试录音url
    isMainRecording: false, // 主录音状态
    isFirstQuestionREC: true // 是否为首次点击问题录音按钮
};
/**
 * 提供面试录音环境下的状态共享，包含录音url，主录音启动状态，是否为首次点击录音按钮
 */
const RecStateContextProvider = props => {
    const [state, changeState] = useReducer(reducer, initState);
    return (
        <RecStateContext.Provider
            value={{
                state,
                changeState
            }}
        >
            {props.children}
        </RecStateContext.Provider>
    );
};

export { RecStateContext, RecStateContextProvider };
